///////图片上传处理
var maxSize = 1624;


var t1;
var imgid = "";
var cout_tyep = 0;
var fair_id = 0;
var cont_id = 0;
//name选择器名称,模拟点击input文件选择框
function upfile(t, name, type = 0, id = 0) {
    imgid = t;
    cout_tyep = type;
    fair_id = t;
    cont_id = id;

    var maxbox = $("#MAX_FILE_SIZE").val();

    if (maxbox != null) maxSize = maxbox;

    document.querySelector(name).click();
}
///选中文件
function onSelectFileChange(userfile) {
    if ($(userfile).val() == "") {

        return;
    }
    UpladFile(userfile);
}

//上传文件
function UpladFile(userfile) {


    var path = userfile.value.toString();


    var perfix = path.substring(path.indexOf('.') + 1);

    var regExp = /(jpg|jpeg|gif|png)/gi;

    if (perfix.match(regExp)) {

        if (!checkFileSize(userfile)) //如果图片文件类型匹配成功则执行文件大小的检测。
        {

            return false;
        }

    } else {

        alert('对不起，请选择图片文件'); return false;

    }



    //定义上传地址
    if (typeof up_url != "undefined") {
        up_url = up_url;
    } else {
        up_url = "upload";
    }

    for (i = 0; i < userfile.files.length; i++) {
        t1 = setTimeout(showprogress, 10);
        var formData = new FormData();
        //选中的第一个文件加入FormData
        formData.append("userfile", userfile.files[i]);
        // console.log(formData);
        var xhr = new XMLHttpRequest();
        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);
        //向服务器发送请求

        // up_url = 'index.php?s=/member/passport/upload';
        xhr.open("POST", up_url);
        xhr.send(formData);
        // console.log(up_url);
        //  return;
    }
    if (ismultiple != null) {

        uploadReload();
    }


}

//展示上传进度
function showprogress() {
    if (imgid != '') {
        $("#demo" + imgid + " .layui-progress").show();
    } else {
        $(".layui-progress").show();
    }
    clearTimeout(t1);
}

//隐藏上传进度
function hideprogress() {
    $(".layui-progress").hide();
    $(name).replaceWith($(name).val('').clone(true));
    clearTimeout(t1);
}
//图片上传进度
function uploadProgress(evt) {
    if (evt.lengthComputable) {
        var percentComplete = Math.round(evt.loaded * 100 / evt.total);
        setProgress(percentComplete);
        if (percentComplete == 100) {
            t1 = setTimeout(hideprogress, 1000);
        }

    } else {
        t1 = setTimeout(hideprogress, 1000);
    }
}

function uploadFailed(evt) {
    alert("上传失败");
}

function uploadCanceled(evt) {
    alert("已经取消上传");
}
//
function imgremove(e) {
    $(e).parent().parent().hide();
    $(e).parent().remove();
}

function setProgress(v) {
    layui.use('element', function () {
        var $ = layui.jquery,
            element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
        //设置50%进度
        element.progress('demo', v + "%")

    });
}
$(".layui-progress").hide();


function checkFileSize(obj) {


    for (i = 0; i < obj.files.item.length; i++) {
        var img = new Image();

        try {

            var imgSize = obj.files.item(i).size;

            return isLarged(imgSize, maxSize);//图片文件大小比对。

        } catch (e) {
            try {

                img.src = obj.value;

                return isLarged(img.fileSize, maxSize);

            } catch (e) {

                obj.select();



                var path = document.selection.createRange.text();

                img.src = path;

                return isLarged(img.fileSize, maxSize);

            }
        }
    }
}


function isLarged(imgSize, maxSize) {


    if ((imgSize / 1024) > maxSize) {

        alert('对不起，您选择的图片文件超过了' + parseInt(maxSize / 1024) + 'M');

        return false;

    } else {

        // alert(parseInt(imgSize / 1024) + 'kb ' + 'This large is OK');

        return true;

    }
} 